﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace YXShop.Web.membercenter.individualinfo
{
    public partial class changecalorie : System.Web.UI.Page
    {
        YXShop.BLL.Member.MemberAccount accountBll = new YXShop.BLL.Member.MemberAccount();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                InitWebControl();
                if (Session["MemberID"]!=null)
                {
                    int uid = Convert.ToInt32(Session["MemberID"]);
                    ViewState["uid"] = uid.ToString();
                    YXShop.Model.Member.MemberAccount model = accountBll.GetModel(uid);
                    if (model != null)
                    {
                        this.lblName.Text = model.UserId.ToString();
                        this.lblCapital.Text = model.Capital.ToString();
                        this.lblCoupons.Text = model.Coupons.ToString();
                        this.lblIntegral.Text = model.Points.ToString();
                        TimeSpan oldtime = new TimeSpan(Convert.ToDateTime(model.PeriodOfValidity).Ticks);
                        TimeSpan newtime = new TimeSpan(Convert.ToDateTime(System.DateTime.Now.ToShortDateString()).Ticks);
                        TimeSpan gap = oldtime.Subtract(newtime).Duration();
                        this.lblDay.Text = gap.Days.ToString();
                    }
                }             
            }
      
       }
        #region 初试化
        private void InitWebControl()
        {
            ChangeHope.WebPage.WebControl.Validate(this.txtNumber,"冲值卡卡号不能为空","isnull","必填","该项为必填项");
            ChangeHope.WebPage.WebControl.Validate(this.txtPassword,"冲值卡密码不能为空","isnull","必填","该项为必填项");
            this.Form.Attributes.Add("onsubmit", "return CheckForm();");
        }
        #endregion

        //冲值事件
        protected void btnOk_Click(object sender, EventArgs e)
        {
            string cardnumber = this.txtNumber.Text.Trim().ToString();
            string password =this.txtPassword.Text.Trim().ToString();
            YXShop.BLL.OrderCard.OrderCardInfo cardBll=new YXShop.BLL.OrderCard.OrderCardInfo();
            YXShop.Model.OrderCard.OrderCardInfo model = cardBll.GetModelByCardNumber(cardnumber);
            string url = YXShop.TemplateAction.Common.UrlStr("memberinfo_show", "uid=" + ViewState["uid"].ToString(), "membercenter/individualinfo/");
            if (model != null)
            {
                if (model.JOCW_Password.Equals(password))
                {
                    if (Convert.ToDateTime(System.DateTime.Now.ToShortDateString()) < Convert.ToDateTime(model.ExpirationDate))
                    {
                        if (Convert.ToInt32(model.WhetherRelease) != 1)
                        {
                            YXShop.Model.Member.MemberAccount account = accountBll.GetModel(Convert.ToInt32(ViewState["uid"]));
                            if(account!=null)
                            {
                                //记录
                                YXShop.BLL.Member.UserInfoNote noteBll = new YXShop.BLL.Member.UserInfoNote();
                                YXShop.Model.Member.UserInfoNote noteModel = new YXShop.Model.Member.UserInfoNote();
                                noteModel.BuckleOrAdd = 0;
                                noteModel.NoteName = "系统";
                                noteModel.NoteDate = Convert.ToDateTime(System.DateTime.Now.ToShortDateString());
                                //冲值卡类型
                                switch (model.Unit.Trim().ToString())
                                {   
                                    case "点":                                      
                                        account.Coupons = Convert.ToDecimal(account.Coupons) + Convert.ToDecimal(model.Point);
                                        accountBll.Update(account);
                                        this.ltlMsg.Text = "操作已成功，您的帐户已冲值"+model.Point.ToString()+"点，请注意查收。<a href=\""+url+"\">点击查看我的帐户</a>";
                                        this.pnlMsg.Visible = true;
                                        this.pnlMsg.CssClass = "actionOk";
                                        noteModel.NoteType = 0;
                                        noteModel.Causation = "充值卡充值" + model.Point + "点点卷";
                                        noteModel.BosomNote = "充值卡充值";
                                        noteModel.TicketCount = Convert.ToDecimal(model.Point);
                                        break;
                                    case "天":
                                        account.PeriodOfValidity = Convert.ToDateTime(account.PeriodOfValidity).AddDays(Convert.ToDouble(model.Point));
                                        accountBll.Update(account);
                                        this.ltlMsg.Text = "操作已成功，您的帐户已冲值" + model.Point.ToString() + "天有效期，请注意查收。<a href=\"" + url + "\">点击查看我的帐户</a>";
                                        this.pnlMsg.Visible = true;
                                        this.pnlMsg.CssClass = "actionOk";
                                        noteModel.NoteType = 2;
                                        noteModel.Causation = "充值卡充值" + model.Point + "天有效期";
                                        noteModel.BosomNote = "充值卡充值";
                                        noteModel.TicketCount = Convert.ToDecimal(model.Point);
                                        break;
                                    case "月":
                                        account.PeriodOfValidity = Convert.ToDateTime(account.PeriodOfValidity).AddMonths(Convert.ToInt32(model.Point));
                                        accountBll.Update(account);
                                        this.ltlMsg.Text = "操作已成功，您的帐户已冲值" + model.Point.ToString() + "个月有效期，请注意查收。<a href=\"" + url + "\">点击查看我的帐户</a>";
                                        this.pnlMsg.Visible = true;
                                        this.pnlMsg.CssClass = "actionOk";
                                        noteModel.NoteType = 2;
                                        noteModel.Causation = "充值卡充值" + model.Point + "个月有效期";
                                        noteModel.BosomNote = "充值卡充值";
                                        noteModel.TicketCount = Convert.ToDecimal(model.Point)*30;
                                        break;
                                    case "年":
                                        account.PeriodOfValidity = Convert.ToDateTime(account.PeriodOfValidity).AddYears(Convert.ToInt32(model.Point));
                                        accountBll.Update(account);
                                        this.ltlMsg.Text = "操作已成功，您的帐户已冲值" + model.Point.ToString() + "年有效期，请注意查收。<a href=\"" + url + "\">点击查看我的帐户</a>";
                                        this.pnlMsg.Visible = true;
                                        this.pnlMsg.CssClass = "actionOk";
                                        noteModel.NoteType = 2;
                                        noteModel.Causation = "充值卡充值" + model.Point + "年有效期";
                                        noteModel.BosomNote = "充值卡充值";
                                        noteModel.TicketCount = Convert.ToDecimal(model.Point) * 365;
                                        break;
                                    case "元":
                                        account.Capital = Convert.ToDecimal(account.Capital) + Convert.ToDecimal(model.Point);
                                        accountBll.Update(account);
                                        this.ltlMsg.Text = "操作已成功，您的帐户已冲值" + model.Point.ToString() + "元，请注意查收。<a href=\"" + url + "\">点击查看我的帐户</a>";
                                        this.pnlMsg.Visible = true;
                                        this.pnlMsg.CssClass = "actionOk";
                                        noteModel.NoteType = 1;
                                        noteModel.Causation = "充值卡充值" + model.Point + "元";
                                        noteModel.BosomNote = "充值卡充值";
                                        noteModel.TicketCount = Convert.ToDecimal(model.Point);
                                        break;
                                    default:
                                        break;
                                }
                                noteModel.UserID = Convert.ToInt32(account.UID);
                                noteModel.Username = account.UserId;
                                int count = cardBll.Amend(model.ID, "whetherRelease",1);
                                noteBll.Add(noteModel);                             
                            }
                        }
                        else
                        {
                            this.ltlMsg.Text = "此卡已使用，请重新输入";
                            this.pnlMsg.Visible = true;
                            this.pnlMsg.CssClass = "actionErr";
                            return;
                        }
                    }
                    else
                    {
                        this.ltlMsg.Text = "此卡已过期，请重新输入";
                        this.pnlMsg.Visible = true;
                        this.pnlMsg.CssClass = "actionErr";
                        return;
                    }
                }
                else
                {
                    this.ltlMsg.Text = "您输入的卡号密码有误，请重新输入";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionErr";
                    return;
                }
              

            }
            else
            {
                this.ltlMsg.Text = "您输入的卡号不存在，请重新输入";
                this.pnlMsg.Visible = true;
                this.pnlMsg.CssClass = "actionErr";
                return;
            }
        }
    }
}
